package com.xmall.service.impl;

import com.xmall.bean.table.*;
import com.xmall.bean.vo.WxSearchIndexVo;
import com.xmall.mapper.KeywordMapper;
import com.xmall.mapper.SearchHistoryMapper;
import com.xmall.service.WxSearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchIndexVo wxSearchIndex() {
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();//待封装再返回的对象
        //1.defaultKeyword:表keyword中的对象 查询条件is_default = 1
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);//is_default = 1
        List<Keyword> defaultKeywords = keywordMapper.selectByExample(keywordExample);
        Keyword defaultKeyword = defaultKeywords.get(0);
        wxSearchIndexVo.setDefaultKeyword(defaultKeyword);

        //2.hotKeywordList:表keyword表中的对象 查询条件is_hot = 1
        //todo 是否是默认hot关键字
        KeywordExample keywordExample1 = new KeywordExample();
        KeywordExample.Criteria criteria1 = keywordExample1.createCriteria();
        //is_hot = 1
        criteria1.andIsHotEqualTo(true);
        List<Keyword> hotKeywords = keywordMapper.selectByExample(keywordExample1);
        wxSearchIndexVo.setHotKeywordList(hotKeywords);

        //3.hotKeywordList:表search_history中的所有对象 查询条件user_id = 当前用户的id
        //登录了就有历史，未登录就没用历史
        //3.hotKeywordList:表search_history中的所有对象  但是仅仅显示keyword字段
        // 遍历获得 keyword信息  放入map中

        //3.1 请求体中有token 可以通过工具类获得user对象 ---> userId
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //user登录了，不为null 就把历史记录返回，前端页面渲染娴熟
        if (user != null) {
            Integer userId = user.getId();
            //3.2查询:userId---->searchHistory对象数组
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            SearchHistoryExample.Criteria criteria2 = searchHistoryExample.createCriteria();
            criteria2.andUserIdEqualTo(userId).andDeletedEqualTo(false);
            List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
            //3.3遍历获得keywords放入hashMap中 并封装

            // TODO
            List<HashMap<String, String>> maps = new ArrayList<>();
            for (SearchHistory searchHistory : searchHistories) {
                String keyword = searchHistory.getKeyword();
                HashMap<String, String> map = new HashMap<>();
                map.put("keyword", keyword);//一个map的keyword唯一
                maps.add(map);
            }
            wxSearchIndexVo.setHistoryKeywordList(maps);
        }

        return wxSearchIndexVo;
    }

    @Override
    public void wxClearHistory() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getId();
        //通过useId取把表search_history(delete为false的)逻辑删除
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        //todo 逻辑删除   遍历userId的searchHistory 把delete设置为true
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        for (SearchHistory searchHistory : searchHistories) {
            searchHistory.setDeleted(true);
            //调用update把delete设置为true
            searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
        }


    }

    @Override//登录与否都能访问
    public List<String> wxSearchHelper(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        List<String> strings = new ArrayList<>();
        for (Keyword keyword1 : keywords) {
            strings.add(keyword1.getKeyword());
        }
        return strings;
    }


}
